package com.lbmb.mdps.workbench.service.impl;

import com.lbmb.mdps.commons.domain.HistoriesVo;
import com.lbmb.mdps.commons.domain.DeliverReceive;
import com.lbmb.mdps.commons.domain.DeliverVo;
import com.lbmb.mdps.commons.domain.UserReceive;
import com.lbmb.mdps.commons.exception.DeliverException;
import com.lbmb.mdps.commons.utils.DateTimeUtil;
import com.lbmb.mdps.commons.utils.SnowFlakeIdUtil;
import com.lbmb.mdps.settings.dao.EmployeeDao;
import com.lbmb.mdps.settings.domain.Employee;
import com.lbmb.mdps.workbench.dao.*;
import com.lbmb.mdps.workbench.domain.*;
import com.lbmb.mdps.workbench.service.DeliverService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 林文塨
 * @date 2022-06-01 2:16
 * description
 */
@Service("deliverService")
public class DeliverServiceImpl implements DeliverService {

    @Resource
    private DeliverDao deliverDao;
    @Resource
    private MedicineDao medicineDao;
    @Resource
    private MedicineTypeDao medicineTypeDao;
    @Resource
    private EmployeeDao employeeDao;
    @Resource
    private SupplierDao supplierDao;
    @Resource
    private WarehouseDao warehouseDao;
    @Resource
    private AuditDao auditDao;
    @Resource
    private ThirdPartyDao thirdPartyDao;

    @Override
    public Integer getDeliverTotal(DeliverReceive deliverReceive) throws DeliverException {
        /*
         * 获得采购记录总条数
         * @author 林文塨
         * @date 2022/6/1 8:22
         * @param [deliverReceive]
         * @return java.lang.Integer
         */
        Integer total = deliverDao.getDeliverTotal(deliverReceive);

        if (total < 0) {
            throw new DeliverException("获取采购订单分页总条数失败！");
        }

        return total;
    }

    @Override
    public List<DeliverVo> deliverPageList(DeliverReceive deliverReceive) throws DeliverException {
        /*
         * 分页获取采购记录
         * @author 林文塨
         * @date 2022/6/1 8:22
         * @param [deliverReceive]
         * @return java.util.List<com.lbmb.mdps.workbench.domain.Deliver>
         */
        List<DeliverVo> deliverVoList = new ArrayList<>();

        List<Deliver> deliverList = deliverDao.deliverPageList(deliverReceive);

        if (deliverList == null) {
            throw new DeliverException("分页获取采购订单列表失败！");
        }

        for (Deliver deliver : deliverList) {

            DeliverVo deliverVo = new DeliverVo();

            /*设置药品*/
            Medicine medicine = medicineDao.getMedicineById(deliver.getMedicine());
            MedicineType medicineType = medicineTypeDao.getMedicineTypeById(medicine.getMedicineType());
            medicine.setMedicineTypeVo(medicineType);
            deliverVo.setMedicine(medicine);

            /*设置基本属性*/
            deliverVo.setId(deliver.getId());
            deliverVo.setNumber(deliver.getNumber());
            deliverVo.setOrderTime(deliver.getOrderTime());
            deliverVo.setState(deliver.getState());
            deliverVo.setAggregateAmount(deliver.getAggregateAmount());

            /*设置下单人*/
            Employee employee = employeeDao.getEmployeeById(deliver.getOrderBy());
            deliverVo.setOrderBy(employee.getName());

            /*设置供应商*/
            Supplier supplier = supplierDao.getSupplierById(deliver.getSupplier());
            deliverVo.setSupplier(supplier.getName());

            /*设置仓库*/
            Warehouse warehouse = warehouseDao.getWarehouseById(deliver.getWarehouse());
            deliverVo.setWarehouse(warehouse.getName());

            /*设置第三方*/
            ThirdParty thirdParty = thirdPartyDao.getThirdPartById(deliver.getThirdParty());
            deliverVo.setThirdParty(thirdParty);

            /*设置审核时间表*/
            List<Audit> auditList = auditDao.getAuditsByAuditOrderId(deliver.getId());
            List<HistoriesVo> historiesVoList = new ArrayList<>();

            /*获得所有审核时间表*/
            for (Audit audit : auditList) {
                HistoriesVo historiesVo = new HistoriesVo();

                if ("待审核".equals(audit.getState())) {
                    historiesVo.setContent("待审核——操作人（" + employee.getName() + ")");
                    historiesVo.setTimestamp(audit.getAuditTime());
                    historiesVo.setSize("large");
                    historiesVo.setType("primary");
                    historiesVo.setIcon("el-icon-warning");
                    historiesVo.setColor("blue");
                } else if ("已拒绝".equals(audit.getState())) {
                    historiesVo.setContent("已拒绝——操作人（" + employee.getName() + ")");
                    historiesVo.setTimestamp(audit.getAuditTime());
                    historiesVo.setSize("large");
                    historiesVo.setType("primary");
                    historiesVo.setIcon("el-icon-error");
                    historiesVo.setColor("red");
                } else if("已通过".equals(audit.getState())) {
                    historiesVo.setContent("已通过——操作人（" + employee.getName() + ")");
                    historiesVo.setTimestamp(audit.getAuditTime());
                    historiesVo.setSize("large");
                    historiesVo.setType("primary");
                    historiesVo.setIcon("el-icon-success");
                    historiesVo.setColor("green");
                }

                historiesVoList.add(historiesVo);
            }

            deliverVo.setHistoriesVoList(historiesVoList);

            deliverVoList.add(deliverVo);
        }

        return deliverVoList;
    }

    @Override
    public void editOrderState(UserReceive userReceive) throws DeliverException {
        /*
         * 更改订单状态，同时增加审核记录
         * @author 林文塨
         * @date 2022/6/1 15:03
         * @param [userReceive]
         * @return void
         */
        int result1 = deliverDao.editOrderState(userReceive);

        if (result1 != 1) {
            throw new DeliverException("通过订单失败！");
        }

        Audit audit = new Audit();
        audit.setId(SnowFlakeIdUtil.generateId());
        audit.setState(userReceive.getState());
        audit.setAuditOrder(Long.valueOf(userReceive.getCurrentOrderId()));
        audit.setAuditBy(Long.valueOf(userReceive.getCurrentOrderId()));
        audit.setAuditTime(DateTimeUtil.getSysTime());

        int result2 = auditDao.addAudit(audit);

        if (result2 != 1) {
            throw new DeliverException("添加审核记录失败！");
        }
    }
}
